package edu.cwnu.enterprisemanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.cwnu.enterprisemanagement.VO.AdminVO;
import edu.cwnu.enterprisemanagement.domain.Admin;
import edu.cwnu.enterprisemanagement.service.AdminService;
import edu.cwnu.enterprisemanagement.mapper.AdminMapper;
import edu.cwnu.enterprisemanagement.untils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author Yang Can
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2025-11-16 14:31:06
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminVO login(String username, String password) {
        QueryWrapper< Admin>query = new QueryWrapper<Admin>();
        query.eq("username", username);
        query.eq("password", password);

        Admin admin = adminMapper.selectOne(query);
        if (admin != null) {
            AdminVO adminVo = new AdminVO();
            adminVo.setId(admin.getId());
            adminVo.setUsername(admin.getUsername());
            adminVo.setRole(admin.getRole());

            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put("id", admin.getId());
            claims.put("username", admin.getUsername());
            claims.put("role", admin.getRole());

            String jwt = JwtUtil.genToken(claims);
            adminVo.setToken(jwt);

            return adminVo;
        }

        return null;
    }
}




